package com.tuanzi.loan.web.bean.galaxycard;

import com.tuanzi.loan.business.entity.galaxycard.GalaxyCardPrivilegeStatus;
import com.tuanzi.loan.business.service.galaxycard.GalaxyCardPrivilegeStatusService;
import com.tuanzi.loan.business.service.galaxycard.impl.GalaxyCardPrivilegeServiceImpl;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFListingBean;

import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPrivilegeStatusSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPrivilegeStatusSearchResponse;
import com.tuanzi.loan.web.lazy.galaxycard.GalaxyCardPrivilegeStatusDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Map;
import java.util.Objects;

/**
 * 权益状态管理
 * @author JinRuiyang 2018/2/9 17:14:51
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class GalaxyCardPrivilegeStatusBean extends JSFListingBean<GalaxyCardPrivilegeStatusSearchRequest, GalaxyCardPrivilegeStatusSearchResponse> {

    @Autowired
    private GalaxyCardPrivilegeStatusService service;

    private Map<String, Long> privilegeList;
    private Map<String, Integer> actionList;
    private GalaxyCardPrivilegeStatus selectedStatus;
    private GalaxyCardPrivilegeStatus addingStatus = new GalaxyCardPrivilegeStatus();

    private Boolean isRedirectAt = false;

    @PostConstruct
    private void init() {
        privilegeList = service.getPrivilegeList();
        actionList = GalaxyCardPrivilegeServiceImpl.ActionConst.toMap();
        String privilegeId = getParamFromRequest("privilege_id");
        $ = new GalaxyCardPrivilegeStatusSearchRequest();
        if (StringUtils.isNotBlank(privilegeId)){
            isRedirectAt = true;
            $.setPrivilegeId(Long.parseLong(privilegeId));
        }
        lazyDataModel = new GalaxyCardPrivilegeStatusDataModel($);
    }

    public void onEditPrivilegeStatus(){
        try {
            service.edit(selectedStatus);
            addSuccessfullyMessage();
        } catch (Exception e){
            addErrorMessage("编辑失败:" + e.getMessage());
        }
    }

    public void onAddPrivilegeStatus(){
        try {
            service.add(addingStatus);
            addSuccessfullyMessage();
        } catch (Exception e){
            addErrorMessage("添加失败:" + e.getMessage());
        }
        addingStatus = new GalaxyCardPrivilegeStatus();
    }

    public void onSelect(GalaxyCardPrivilegeStatusSearchResponse response){
        selectedStatus = service.findById(response.getPk());
    }

    public void onReset() {
        $ = new GalaxyCardPrivilegeStatusSearchRequest();
        lazyDataModel = new GalaxyCardPrivilegeStatusDataModel($);
    }

    public void onRefresh(){
        lazyDataModel = new GalaxyCardPrivilegeStatusDataModel($);
        addSuccessfullyMessage();
    }

    public void onDelete(){
        if (Objects.nonNull(selectedStatus))
            service.delete(selectedStatus.getId());
    }
}